<?php

namespace app\api\controller\Product;

use think\Controller;

class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->CartModel = model('Product.Cart');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);


            $cartid = $this->request->param('cartid',0);

            $where = [];

            if(!empty($userid))
            {
                $where['userid'] = $userid;
            }

            if(!empty($cartid))
            {
                $where['cart.id'] = ['in',$cartid];
            }

            $CartList = $this->CartModel->with('product')->where($where)->select();

            if(!$CartList)
            {
                $this->error('获取购物车的数据失败');
                exit;
            }

            $this->success('返回购物车数据',null,$CartList);
        }
    }

    public function add()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);
            $proid = $this->request->param('proid',0);
            $num = $this->request->param('num',1);

            // 查询该商品库存
            $product = $this->ProductModel->find($proid);

            if($product['stock'] <= 0 )
            {
                $this->error('该商品的库存不足');
                exit;
            }

            // 查询该用户是否是VIP
            $user = $this->UserModel->find($userid);

            if($user['vip'] > 0)
            {
                $price = $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'];
                
            }else{
                $price = $product['price'];
            }
            
            


            $data = [
                'userid' => $userid,
                'proid' => $proid,
                'price' => $price,
                'nums' => $num,
                'total' => $price
            ];

            // 查询购物车表是否有该用户以及该商品的记录
            $cart = $this->CartModel->where(['userid' => $userid,'proid' => $proid])->find();

            if($cart)
            {
                // 购物车已经存在该商品
                $data['nums'] = $cart['nums'] + 1;
                $data['total'] = bcmul($price,$data['nums']);
                $data['id'] = $cart['id'];

                $result = $this->CartModel->isUpdate(true)->save($data);
            }else{
                // 新增
                $result = $this->CartModel->save($data);
            }

            if($result === FALSE)
            {
                $this->error('添加购物车失败，请重新添加');
                exit;
            }else{
                $this->success('添加购物车成功，是否去购物车下单？','/product/cart/index');
                exit;
            }
        }
    }

    public function edit()
    {
        if($this->request->isPost())
        {
            $cartid = $this->request->param('cartid',0);
            $nums = $this->request->param('nums',0);

            $cart = $this->CartModel->find($cartid);

            if(!$cart)
            {
                $this->error('商品不存在');
                exit;
            }

            $total = bcmul($nums,$cart['price']);

            $data = [
                'id' => $cartid,
                'nums' => $nums,
                'total' => $total
            ];

            $result = $this->CartModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error('更新商品数量失败');
                exit;
            }else{
                $this->success('更新商品数量成功');
                exit;
            }
        }
    }

    public function del()
    {
        if($this->request->isPost())
        {
            $cartid = $this->request->param('cartid',0);

            $cart = $this->CartModel->find($cartid);

            if(!$cart)
            {
                $this->error('该条记录不存在，无法删除');
                exit;
            }

            $result = $this->CartModel->where('id',$cartid)->delete();

            if($result === FALSE)
            {
                $this->error('删除失败,请重新选择');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }
    public function now()
    {

            halt($this->request->isPost());
    }

    
}
